package com.znjc.device.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    /**
     * 获取两个日期之间的所有日期 (年月日)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<String> getBetweenDate(Date startDate, Date endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        //用Calendar 进行日期比较判断
        Calendar calendar = Calendar.getInstance();
        while (startDate.getTime() <= endDate.getTime()) {
            // 把日期添加到集合
            list.add(sdf.format(startDate));
            // 设置日期
            calendar.setTime(startDate);
            //把日期增加一天
            calendar.add(Calendar.DATE, 1);
            // 获取增加后的日期
            startDate = calendar.getTime();
        }
        return list;
    }

    /**
     * @return
     * @description: 月前
     */
    private static Date getMonthData(Date data, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(data);
        cal.add(Calendar.MONTH, num);
        return cal.getTime();
    }

    /**
     * @return
     * @description: 周前
     */
    private static Date getWeeKData(Date data, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(data);
        cal.add(Calendar.WEEK_OF_YEAR, num);
        return cal.getTime();
    }

    /**
     * @param endDate
     * @param num
     * @param type    0月 1周
     * @return
     */
    public static List<String> getBetweenDate(Date endDate, int num, int type) {
        Date startDate = null;
        if (0 == type) {
            startDate = getMonthData(endDate, num);
        } else if (1 == type) {
            startDate = getWeeKData(endDate, num);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        //用Calendar 进行日期比较判断
        Calendar calendar = Calendar.getInstance();
        while (startDate.getTime() <= endDate.getTime()) {
            // 把日期添加到集合
            list.add(sdf.format(startDate));
            // 设置日期
            calendar.setTime(startDate);
            //把日期增加一天
            calendar.add(Calendar.DATE, 1);
            // 获取增加后的日期
            startDate = calendar.getTime();
        }
        return list;
    }

    /**
     * 获取当前时间前n个月的月份
     *
     * @param num
     * @return
     */
    public static String getDate(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 创建 Calendar 对象
        Calendar calendar = Calendar.getInstance();
        // 将日期设置为当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        // 将日期向前移动一个月
        calendar.add(Calendar.MONTH, num);
        Date m = calendar.getTime();
        return format.format(m);
    }

    public static String getDataByDay(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 创建 Calendar 对象
        Calendar calendar = Calendar.getInstance();
        // 将日期设置为当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        // 将日期向前移动一个月
        calendar.add(Calendar.DAY_OF_MONTH, num);
        Date m = calendar.getTime();
        return format.format(m);
    }

    /**
     * 字符串转Timestamp
     *
     * @param dateString
     * @return
     * @throws Exception
     */
    public static Timestamp StringToTimestamp(String dateString) throws Exception {

        return Timestamp.valueOf(dateString);
    }

    /**
     * Date转Timestamp
     * @param date
     * @return
     * @throws Exception
     */
    public static Timestamp DateToTimestamp(Date date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        return StringToTimestamp(dateString);
    }

    /**
     * Timestamp转字符串
     *
     * @param timestamp
     * @return
     * @throws Exception
     */
    public static String TimestampToString(Timestamp timestamp) throws Exception {
        if(null == timestamp){
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        // 转换为 LocalDateTime
        LocalDateTime localDateTime = timestamp.toLocalDateTime();
        String formattedDate = localDateTime.format(formatter);
        return formattedDate;
    }

    /*public static void main(String[] args) {
        System.out.println(getDate(-1));
    }*/
}
